package com.coding.service.impl;


import com.coding.base.pojo.BaseModel;
import com.coding.dao.PoemDao;
import com.coding.dao.UserDao;
import com.coding.pojo.Poem;
import com.coding.pojo.PoemBasic;
import com.coding.pojo.User;
import com.coding.service.BackstageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class BackstagerImpl implements BackstageService {
    @Autowired
    UserDao userDao;
    @Autowired
    PoemDao poemDao;

    @Override
    public void getnewuserid(BaseModel baseModel) throws Exception {
        //获得最新id
        int user_id = userDao.getnewuser_id() + 1;
        if (user_id == 0) {
            baseModel.setData("");
            baseModel.setMessage("获取最新用户id失败");
            baseModel.setResultCode(0);
            return;
        }
        baseModel.setMessage("成功获取最新用户id");
        baseModel.setResultCode(1);
        baseModel.setData(user_id);
    }

    @Override
    public void getuserlist(BaseModel baseModel) throws Exception {
        List<User> users = userDao.getuserlist();
        if (users.isEmpty()) {
            baseModel.setMessage("查询用户列表失败");
            baseModel.setResultCode(0);
            baseModel.setData("");
            return;
        }
        baseModel.setData(users);
        baseModel.setResultCode(1);
        baseModel.setMessage("查询用户列表成功");

    }

    @Override
    public void insertuser(User user, BaseModel baseModel) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        user.setRegister_time(df.format(new Date()));// new Date()为获取当前系统时间
        //设置默认头像
        if (user.getUser_image() == 0) {
            user.setUser_image(16);
        }
        int flag = userDao.insertuser(user);
        if (flag == 0) {
            baseModel.setMessage("增添用户失败");
            baseModel.setResultCode(0);
            baseModel.setData("");
            return;
        }
        baseModel.setResultCode(1);
        baseModel.setMessage("增添用户成功");
        baseModel.setData("");
    }

    @Override
    public void deleteuser(int user_id, BaseModel baseModel) throws Exception {
        int flag = userDao.deleteuser(user_id);
        if (flag == 0) {
            baseModel.setMessage("删除用户失败");
            baseModel.setResultCode(0);
            baseModel.setData("");
            return;
        }
        baseModel.setResultCode(1);
        baseModel.setMessage("删除用户成功");
        baseModel.setData(user_id);
    }

    @Override
    public void updateuser(User user, BaseModel baseModel) throws Exception {
        //设置默认头像
        if (user.getUser_image() == 0) {
            user.setUser_image(16);
        }
        int flag = userDao.updateuser(user);

        if (flag == 0) {
            baseModel.setMessage("更改用户失败");
            baseModel.setResultCode(0);
            baseModel.setData("");
        }
        baseModel.setResultCode(1);
        baseModel.setMessage("更改用户成功");
        baseModel.setData(user.getUser_id());
    }

    @Override
    public void changeuser_status(int user_id, BaseModel baseModel) throws Exception {
        int flag = userDao.changeuser_status(user_id);
        int status = userDao.getuser_status(user_id);
        if (flag == 0) {
            baseModel.setMessage("更改用户状态失败");
            baseModel.setResultCode(0);
            baseModel.setData("");
        }
        baseModel.setResultCode(1);
        baseModel.setMessage("更改用户状态成功,用户现在的状态是" + status);

        baseModel.setData(status);
    }

    @Override
    public void getpoemlist(BaseModel baseModel) throws Exception {
        List<PoemBasic> poemList = poemDao.queryAll();
        if (poemList.isEmpty()) {
            baseModel.setData("");
            baseModel.setResultCode(0);
            baseModel.setMessage("获取诗词列表失败");
            return;
        }
        baseModel.setData(poemList);
        baseModel.setResultCode(1);
        baseModel.setMessage("获取诗词列表成功");
    }

    @Override
    public void getnewpoemid(BaseModel baseModel) throws Exception {
        //获得最新id=max+1
        int poem_id = poemDao.getNewPoem_id() + 1;
        if (poem_id == 0) {
            baseModel.setData("");
            baseModel.setResultCode(0);
            baseModel.setMessage("获取诗词id失败");
            return;
        }
        baseModel.setData(poem_id);
        baseModel.setResultCode(1);
        baseModel.setMessage("获取诗词id成功");
    }

    @Override
    public void insertpoem(Poem poem, BaseModel baseModel) throws Exception {
        if (poem.getPoem_imageid() == 0) {
            poem.setPoem_imageid(98);
        }
        int flag = poemDao.insertSelective(poem);

        if (flag == 0) {
            baseModel.setMessage("新增诗词失败");
            baseModel.setResultCode(0);
            baseModel.setData("");
            return;
        }
        baseModel.setData(poem.getPoem_id());
        baseModel.setResultCode(1);
        baseModel.setMessage("新增诗词成功");
    }

    @Override
    public void updatepoem(Poem poem, BaseModel baseModel) throws Exception {
        if (poem.getPoem_imageid() == 0) {
            poem.setPoem_imageid(98);
        }
        int flag = poemDao.updateByPrimaryKeySelective(poem);

        if (flag == 0) {
            baseModel.setMessage("更新诗词失败");
            baseModel.setResultCode(0);
            baseModel.setData("");
            return;
        }
        baseModel.setData(poem.getPoem_id());
        baseModel.setResultCode(1);
        baseModel.setMessage("更新诗词成功");
    }

    @Override
    public void deletepoem(int poem_id, BaseModel baseModel) throws Exception {
        int flag = poemDao.deleteByPrimaryKey(poem_id);
        if (flag == 0) {
            baseModel.setMessage("删除诗词失败");
            baseModel.setResultCode(0);
            baseModel.setData("");
            return;
        }
        baseModel.setData(poem_id);
        baseModel.setResultCode(1);
        baseModel.setMessage("删除诗词成功");
    }



}
